package top.kaoshanji.p1ch04a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.util.Date;

/**
 * 代码清单 p1ch04a-10：跟踪格式化结果中不同部分字符串的位置的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/15 19:21
 */
public class DateFormatExample01 {

    private static final Logger logger = LogManager.getLogger(DateFormatExample01.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p1ch04a-10：跟踪格式化结果中不同部分字符串的位置的示例...");

        DateFormat format = DateFormat.getDateInstance(DateFormat.FULL);
        Date date = new Date();
        StringBuffer result = new StringBuffer();
        FieldPosition dayField = new FieldPosition(DateFormat.DAY_OF_WEEK_FIELD);

        format.format(date, result, dayField);
        String day = result.substring(dayField.getBeginIndex(), dayField.getEndIndex());

        // 值为星期几的本地化形式
        logger.info("...day:{}...", day);

    }

}
